// pages/order/order.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderNum: '',
    payPrice: '',
    createTime: '',
    show: false,
    totalPrice: '',
    phone: '',
    show1: false,
    loadingText: '支付中...',
  },


  onSubmit: function () {
    this.setData({
      show: true
    });
  },

  onConfirm: function () {
    var order = {
      phone: this.data.phone,
      totalPrice: this.data.totalPrice

    };
    this.setData({
      show1: true
    });
    wx.request({
      url: 'http://192.168.2.10:8250/updateStatus?orderNum='+this.data.orderNum,
      method:'GET',
      success:(res) =>{
        if (res.data.code === 200) {

        }
      }
    })
    const id = wx.getStorageSync('selectedCoupon')
    if(id!=null){
      wx.request({
        url: 'http://192.168.2.10:8099/updateUsersCoupon/'+id,
        method:'GET',
        success:(res) => {
          if (res.data.code === 200) {
            wx.removeStorageSync('selectedCoupon');
          }
        }
      })
    }
    wx.request({
      url: 'http://192.168.2.10:8010/updatePhoneBill/',
      method: 'POST',
      data: order,
      success: (res) => {
        if (res.data.code === 200) {
          wx.removeStorageSync('selectedCoupon');
          setTimeout(() => {
            wx.navigateTo({
              url: '/pages/recharge/recharge'
            });
            wx.showToast({
              title: '充值成功',
              icon: 'success',
              duration: 2000
            });
          }, 3000); // Wait for 3 seconds  
        }
      }
    })
  },

  onClickLeft() {
    wx.navigateBack({
      delta: 1
    })
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    const orderNum = wx.getStorageSync('orderNum');
  
    this.setData({
      orderNum: orderNum,
    })
    wx.request({
      url: 'http://192.168.2.10:8250/queryOrderByOrderNum?orderNum=' + this.data.orderNum,
      method: 'GET',
      success: (res) => {
        if (res.data.code === 200) {
          let date = new Date(res.data.data.createTime);
          date.setHours(date.getHours() + 16);
          this.setData({
            payPrice: res.data.data.payPrice,
            createTime: new Date(date).toLocaleString(),
            totalPrice: res.data.data.totalPrice,
            phone: res.data.data.userTelephone
          })
          console.log(this.data.totalPrice)
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})